package kr.ac.ssu.ailab.Control;

import kr.ac.ssu.ailab.Model.Data.Cell;
import kr.ac.ssu.ailab.View.SeqTest;

/**
 * 
 * 맵을 구성하는 기본 요소인 셀을 컨트롤 하는 클래스
 * @author Rivers
 *
 */
public abstract class CellControl
{

	public static int threatenAlpha = 100;

	/* Constant Values define Cell Color( also type ) */

	public static final int NORMAL = 0x001;
	public static final int HARD = 0x002;
	public static final int OBSTACLE = 0x003;
	public static final int FINISH_CELL = 0x005;

	public static final int START_CELL = 0x004;
	public static final int TRANS_CELL = 0x006;
	public static final int OPENLIST = 0x007;
	public static final int CLOSEDLIST = 0x008;
	public static final int MIDLIST = 0x009;
	public static final int SELECT = 0x010;
	public static final int PART_OF_PATH = 0x011;
	public static final int SECOND_TRANS_CELL = 0x012;
	public static final int STAND_CELL = 0X013;
	public static final int CANDI_STAND_CELL = 0X014;
	public static final int THREATEN_AREA = 0x015;

	/* Constant Values define Cell's cost */
	public static final int START_COST = 0x000;
	public static final int FINISH_COST = 0x000;
	public static final int NORMAL_COST = 0x001;
	public static final int HARD_COST = 0x00A;
	public static final double OBSTACLE_COST = Double.MAX_VALUE;
	public static final int THREATEN_AREA_COST = 0x0FF;

	/**
	 * Cell 의 Type 및 코스트를  설정하는 메소드
	 * @param cell
	 * @param type
	 * @param cost
	 */
	public static void setCellType(Cell cell, int type, double cost)
	{
		cell.setType(type);
		cell.setWeight(cost);

	}

	/**
	 * Cell 의 Type을  설정하는 메소드
	 */
	public static void setCellType(Cell cell, int type)
	{
		cell.setType(type);

	}

	/**
	 * 스타트 셀을  설정하는 메소드
	 * @param cell
	 */
	public static void setStartCell(Cell cell)
	{
		if (SeqTest.mapControl.getStartCell() == null)
		{
			SeqTest.mapControl.setStartCell(cell);
			cell.setType(START_CELL);

		}
		else if (SeqTest.mapControl.getStartCell() != cell)
		{
			SeqTest.mapControl.getStartCell().setType(NORMAL);
			SeqTest.mapControl.getStartCell().setWeight(NORMAL_COST);

			SeqTest.mapControl.setStartCell(cell);
			SeqTest.mapControl.getStartCell().setType(START_CELL);
			SeqTest.mapControl.getStartCell().setWeight(START_COST);

		}
	}


	/**
	 * Finish Cell을 설정하는 메소드
	 * @param cell
	 */
	public static void setFinishCell(Cell cell)
	{
		if (SeqTest.mapControl.getFinishCell() == null)
		{
			SeqTest.mapControl.setFinishCell(cell);
			cell.setType(FINISH_CELL);

		}
		else if (SeqTest.mapControl.getFinishCell() != cell)
		{
			SeqTest.mapControl.getFinishCell().setType(NORMAL);
			SeqTest.mapControl.getFinishCell().setWeight(NORMAL_COST);

			SeqTest.mapControl.setFinishCell(cell);
			SeqTest.mapControl.getFinishCell().setType(FINISH_CELL);
			SeqTest.mapControl.getFinishCell().setWeight(FINISH_COST);

		}
	}

	/**
	 * Obstacle Cell 을  설정하는 메소드
	 * @param cell
	 * @param type
	 * @param cost
	 */
	public static void setObstacleCell(Cell cell, int type, double cost)
	{
		cell.setType(type);
		cell.setObstacle(true);
		cell.setWeight(cost);
		// System.out.println("weight" + cost + ", " + cell.getWeight());

	}

	/**
	 * 클러스터의 연결을 담당하는 StandCell을 설정한다.
	 * 
	 * @param cell
	 */
	public static void setStandCell(Cell cell)
	{
		cell.setType(STAND_CELL);

	}

	/**
	 * NormalCell 로 설정하는 메소드
	 * 
	 * @param cell
	 */
	public static void setNormalCell(Cell cell)
	{
		cell.setType(NORMAL);
		cell.setWeight(NORMAL_COST);
		cell.setParentPoint(null);
		cell.setObstacle(false);

	}

	/**
	 * 셀의 Parent Point를 Clear 하는 메소드
	 * @param cell
	 */
	public static void clearParentPoint(Cell cell)
	{
		cell.setParentPoint(null);
	}

	/**
	 * Cell의 모든 속성을 초기화 시키는 메소드
	 * 
	 * @param x
	 * @param y
	 */
	public static void clearCell(int x, int y)
	{
		Cell[][] cell = null;
		cell = SeqTest.mapControl.getCellArray();
		setNormalCell(cell[x][y]);
	}
	
}
